/*
 * Scaffold Application Framework
 *
 * Copyright (c) 2010 Sii Sp. z o.o. All rights reserved.
 */
package com.rstrulak.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.security.core.GrantedAuthority;

import com.rstrulak.constant.FieldLength;

/**
 * Permissions available in the application.
 * 
 * @author Jakub Ciechan
 * @author Wojciech Kochman
 * @version 1.1
 */
@Entity
@Table(name = "PERMISSIONS")
public class Permission implements java.io.Serializable, GrantedAuthority {
    /**
     * Id generated for serialization purposes.
     */
    private static final long serialVersionUID = -7278234276353822311L;
    /**
     * Permission id.
     */
    private Long id;
    /**
     * Human-readable "natural" permission identifier..
     */
    private String code;
    /**
     * Permission name.
     */
    private String name;

    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Permission() {
    }

    /**
     * Getter for id.
     * 
     * @return the id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", precision = 22, scale = 0)
    public Long getId() {
        return this.id;
    }

    /**
     * Getter for code.
     * 
     * @return the code
     */
    @Column(name = "CODE", nullable = false, unique = true, length = FieldLength.SHORT_TEXT)
    public String getCode() {
        return code;
    }

    /**
     * Getter for name.
     * 
     * @return the name
     */
    @Column(name = "NAME", nullable = false, length = FieldLength.LONG_TEXT)
    public String getName() {
        return this.name;
    }

    /**
     * Setter for id.
     * 
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * Setter for code.
     * 
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * Setter for name.
     * 
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.springframework.security.GrantedAuthority#getAuthority()
     */
    @Override
    @Transient
    public String getAuthority() {
        return code;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Permission == false) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        Permission other = (Permission) obj;

        return new EqualsBuilder().append(this.id, other.id).append(this.code, other.code).isEquals();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.id).append(this.code).toHashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return new ToStringBuilder(this).append("id", this.id).append("code", this.code).append("name", this.name).toString();
    }
}
